package org.jeecg.common.util.dingtalk;

import java.util.ArrayList;
import java.util.List;

import org.jeecg.common.constant.ProcessConstant;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiInactiveUserGetRequest;
import com.dingtalk.api.request.OapiInactiveUserV2GetRequest;
import com.dingtalk.api.request.OapiUserCreateRequest;
import com.dingtalk.api.request.OapiUserDeleteRequest;
import com.dingtalk.api.request.OapiUserGetByMobileRequest;
import com.dingtalk.api.request.OapiUserUpdateRequest;
import com.dingtalk.api.response.OapiInactiveUserGetResponse;
import com.dingtalk.api.response.OapiInactiveUserV2GetResponse;
import com.dingtalk.api.response.OapiUserCreateResponse;
import com.dingtalk.api.response.OapiUserDeleteResponse;
import com.dingtalk.api.response.OapiUserGetByMobileResponse;
import com.dingtalk.api.response.OapiUserUpdateResponse;
import com.taobao.api.ApiException;

import me.kagura.JSONQuery;
import me.kagura.JsonResult;
import me.kagura.exception.FieldNotExistException;
import me.kagura.exception.TypeNotMismatchException;

/**
 * @author heckerstone
 * @date 2020-08-14
 * @description 钉钉用户接口操作方法类
 *
 */
public class DingUserUtils {
	
	/**
	 * @author heckerstone
	 * @date 2020-08-14
	 * @description 为活跃用户数据查询
	 * @param access_token 
	 * @param query_date 查询日期 如：20200723
	 * @param offset 数据偏移也即第一条的坐标
	 * @param size 获取数量
	 */
	public static void getInactiveUser(String access_token,String query_date,String offset,String size) {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETINACTIVEUSER);
			OapiInactiveUserGetRequest req = new OapiInactiveUserGetRequest();
			req.setQueryDate("20200723");
			req.setOffset(0L);
			req.setSize(10L);
			OapiInactiveUserGetResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author heckerstone
	 * @date 2020-08-14
	 * @description 查询企业下未活跃用户或活跃用户列表数据
	 * @param access_token 
	 * @param query_date 查询日期 如：20200723
	 * @param offset 数据偏移也即第一条的坐标
	 * @param size 获取数量
	 */
	public static void getInactiveUserV2(String access_token,String query_date,String offset,String size) {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETINACTIVEUSER);
			OapiInactiveUserV2GetRequest req = new OapiInactiveUserV2GetRequest();
			req.setIsActive(false);
			req.setOffset(0L);
			req.setSize(10L);
			req.setQueryDate("20200723");
			OapiInactiveUserV2GetResponse rsp = client.execute(req, "20d3dd21b57c3d618e0b0935ac887c3f");
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author heckerstone
	 * @date 2020-08-14
	 * @description  创建钉钉用户信息
	 * @param userId 用户编号，企业内唯一
	 * @param jobNumber 用户工号，与本地数据库关联
	 * @param mobile 手机号
	 * @param department 部门，如果属于多个部门则如："[1,2,3]"
	 * @param name 名称
	 */
	public static String create(String userId,String jobNumber,String mobile,String department,String name,String position) {
		String dUserId="";
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_CREATEUSER);
			OapiUserCreateRequest req = new OapiUserCreateRequest();
			req.setUserid(userId);
			req.setJobnumber(jobNumber);
			req.setMobile(mobile);
			System.out.println("department======"+department);
			req.setDepartment(department);
			req.setName(name);
			req.setPosition(position);
			OapiUserCreateResponse rsp = client.execute(req, DingTalkUtils.get_token());
			System.out.println("获取到的结果===="+rsp.toString());
			JsonResult jsonResult = JSONQuery.select(rsp.getBody(), "userid");
			dUserId=jsonResult.toString();
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		} catch (TypeNotMismatchException e) {
			e.printStackTrace();
		} catch (FieldNotExistException e) {
			e.printStackTrace();
		}
		return dUserId;
	}
	
	/**
	 * 
	* @Title: deleteUser 
	* @Description: 删除钉钉用户 
	* @author heckerstone heckerstone@163.com
	* @date Aug 14, 2020 5:32:14 PM 
	* @param @param userId 钉钉用户编号
	* @param @return    设定文件 
	* @return boolean    返回类型  成功返回true，否则返回false
	* @throws
	 */
	public static boolean deleteUser(String userId) {
		boolean flag=false;
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_DELETEUSER);
			OapiUserDeleteRequest req = new OapiUserDeleteRequest();
			req.setUserid(userId);//钉钉用户编号
			req.setHttpMethod("GET");
			OapiUserDeleteResponse rsp = client.execute(req, DingTalkUtils.get_token());
//			System.out.println(rsp.getBody());
			flag=true;
		} catch (ApiException e) {
			flag=false;
		}
		return flag;
	}
	
	/**
	 * 
	* @Title: getUserByMobile 
	* @Description: 根据手机号查询用户编号
	* @author heckerstone heckerstone@163.com
	* @date Aug 15, 2020 8:37:49 AM 
	* @param mobile    用户手机号 
	* @return void    返回类型 
	* @throws
	 */
	public static String getUserByMobile(String mobile) {
		String userid="";
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETUSERBYMOBILE);
			OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
			req.setMobile(mobile);//用户手机号
			req.setHttpMethod("GET");
			OapiUserGetByMobileResponse rsp = client.execute(req, DingTalkUtils.get_token());
//			System.out.println(rsp.getBody());
			//解析返回的json数据
			JsonResult jsonResult = JSONQuery.select(rsp.getBody(), "userid");
			userid=jsonResult.toString();
		} catch (ApiException e) {
			userid="";
		} catch (TypeNotMismatchException e) {
			userid="";
		} catch (FieldNotExistException e) {
			userid="";
		}
		return userid;
	}
	
	/**
	 * 
	* @Title: updateUser 
	* @Description: 更新用户信息
	* @author heckerstone heckerstone@163.com
	* @date Aug 17, 2020 8:37:48 AM 
	* @param @param userId
	* @param @param jobNumber 工号
	* @param @param mobile 手机号
	* @param @param department 部门ID列表
	* @param @param name 部门名称
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	public static boolean updateUser(String userId,String jobNumber,String mobile,List<Long> department,String name,String position){
		boolean flag=false;
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_UPDATEUSE);
			OapiUserUpdateRequest req = new OapiUserUpdateRequest();
			req.setUserid(userId);
			req.setJobnumber(jobNumber);
			req.setMobile(mobile);
			req.setPosition(position);
			System.out.println("department======"+department);
			//String 转为List<Long>
			req.setDepartment(department);
			req.setName(name);
			OapiUserUpdateResponse rsp = client.execute(req, DingTalkUtils.get_token());
			flag=true;
//			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			flag=false;
		}
		return flag;
	}
	
	public static void getUserByUserId() {
		
	}
	
	public static void main(String[] args) {
		create("eba5edb7667c482b9f38177266daa47d", "202008150006", "15662522256", "[1,395617275]", "test55","");
	}
	

}
